
				********************************************************************
				* 		Enhanced-Home Food Production Program in Burkina Faso 	   *
				*                                         				           *      
				* STATA Version 13												   *
				********************************************************************

/*				
clear all
*** define globals ***

	di "`c(pwd)'"

	global dir=subinstr(`"`c(pwd)'"',"\do","",.)

	global data2010 `"$dir/data/Round 1 2010"'
	global data2012 `"$dir/data/Round 2 2012"'
	global data2013 `"$dir/data/Round 3 2013"'
	global do `"$dir/do"'

* open clean file *
	use "$data2012/R2_Section_24.dta", clear
	*/
* merge with cover page to get interview date *

	merge m:1 hhid using "$data2012/R2_Section_0.dta", keepusing(interview_month1 interview_day1)
	keep if _m==3 // 34 from using didn't merge
	drop _m

* rename and relabel vars *	
	rename interview_month1 intdate_month
	rename interview_day1 intdate_day
	gen intdate_year=2012
	
	gen intdate=mdy(intdate_month, intdate_day, intdate_year)
	format intdate %td
	lab var intdate "Interview date" 
	
	tab intdate, m nol

* double check for duplicates and unique IDs * 		
	cap isid hhid idp_child
		* missing ones *
	
	drop if hhid==. | idp_child==.
		
	duplicates report
		* no duplicates *

* cleaning vars *
	
*** date of birth *** 

	tab1 s24_q3a s24_q3b s24_q3c, m nol
		* 725 missing days, 462 missing months, 300 missing year * 
		
		* create DOB * 
			* verify DOB will be correct * 
			 cap assert s24_q3a<=30 if !missing(s24_q3a) & (s24_q3b==4 | s24_q3b==6 | s24_q3b==9 | s24_q3b==11)
				* 5 contradictions *
				*br if s24_q3a>30 & (s24_q3b==4 | s24_q3b==6 | s24_q3b==9 | s24_q3b==11) &  !missing(s24_q3a)
				
				replace s24_q3a=30 if s24_q3b==11 & hhid==020322
				replace s24_q3a=30 if s24_q3b==11 & hhid==020322
				replace s24_q3a=30 if s24_q3b==11 & hhid==030501
				replace s24_q3a=30 if s24_q3b==11 & hhid==116001
				replace s24_q3a=30 if s24_q3b==9 & hhid==202018
				
			assert s24_q3a<=31 if !missing(s24_q3a) & (s24_q3b==1 | s24_q3b==3 | s24_q3b==5 | s24_q3b==7 | s24_q3b==8 | s24_q3b==10 | s24_q3b==12)
			cap assert s24_q3a<=29 if !missing(s24_q3a) & s24_q3b==2
				* 1 contradiction * 
				*br if s24_q3a>29 & (s24_q3b==2) & !missing(s24_q3a)

				replace s24_q3a=29 if s24_q3b==2 & hhid==031231
				
		gen birthdate = mdy(s24_q3b, s24_q3a, s24_q3c)
		format birthdate %td
			* 263 with missing days * 
		
		sort hhid idp_mother idp_child	
		
		* merge in birthdate from round 1 to eliminate inconsistencies with ages in r2. LB Nov 21, 2013
		tempfile r1 r2
		
		save `r2', replace 
		
		use "$data2010/R1_Section_24", clear
		do "$do/analysis r1 s24_v1"
		
		sort hhid idp_mother idp_child 
		
		save `r1', replace
		
		use `r2', clear 
		
		merge 1:1 hhid idp_mother idp_child using `r1', keepusing(birthdate) update replace 
		drop if _m==2
		
		lab var birthdate "Date of birth" 
		
		
		* generate random DOB if day is missing * 
		gen date_born_rand=1 if s24_q3a==. 
		replace date_born_rand=0 if s24_q3a~=. 
		tab date_born_rand
			* ok *
		
		lab var date_born_rand "Child birth date randomly created"
		label values date_born_rand yesno

		* If birth date and interview date have the same month and year, and birth day is missing, generate a birth date that is between 1 and the interview date * 
		* Can end up with age in days = 0 if birth date and measurement date are equal, which could happen even though it's unlikely * 

		gen s24_q3a_new=round(uniform()*intdate_day+1,1) if (s24_q3b==intdate_month & intdate_year==s24_q3c & s24_q3a==.)
		tab s24_q3a_new, m
		replace s24_q3a=s24_q3a_new if (s24_q3b==intdate_month & intdate_year==s24_q3c & s24_q3a==.)

		* If birth date and measurement date are different, just replace the missing birth date with a random integer controlling for the length of the month and leap years *
		replace s24_q3a= round(uniform()*29+1,1) if s24_q3a==. & (s24_q3b==4 | s24_q3b==6 | s24_q3b==9 | s24_q3b==11) 
		replace s24_q3a= round(uniform()*30+1,1) if s24_q3a==. & (s24_q3b==1 | s24_q3b==3 | s24_q3b==5 | s24_q3b==7 | s24_q3b==8 | s24_q3b==10 | s24_q3b==12) 
		replace s24_q3a= round(uniform()*28+1,1) if s24_q3a==. & s24_q3b==2

		tab s24_q3a, m
		
		
		* update DOB *
		replace birthdate = mdy(s24_q3b, s24_q3a, s24_q3c)
		format birthdate %td

		tab birthdate, m
			* 1 missing. drop them *
		

*** child height *** 
	
	tab s24_q7, m nol
	replace s24_q7=. if (s24_q7<20 | s24_q7>120)
	
*** child weight *** 
	
	replace s24_q5=. if s24_q5<35
	gen ch_weight=s24_q5-s24_q6 
	tab ch_weight, m

	lab var ch_weight "Child's weight (in kg)"
	
*** create age vars ***

	gen ch_ageda=intdate-birthdate  // 548 missing ages
	tab ch_ageda, m
	
	gen ch_agemo=ch_ageda/(365.25/12)
	tab ch_agemo, m
	
	label variable ch_ageda "Child age in days"
	label variable ch_agemo "Child age in months (aged/30.437)"
	
*** rename vars to convention ****

	rename s24_q4 ch_sex
	rename s24_q7 ch_height

	
*** drop unnecessary vars **** 	
	drop s24_q3a s24_q3b s24_q3c s24_q5 s24_q6 intdate_year intdate_month intdate_day s24_q3a_new

	order hhid idp_mother idp_child intdate birthdate ch_sex ch_ageda ch_agemo ch_weight ch_height date_born_rand
	
	
*** creating 1, 3, and 6 motnh age groups ***
	
	label drop ch_age1mo ch_age3mo ch_age6mo
		
	* 6 months * 
	egen ch_age6mo=cut(ch_agemo), at(0(6)60) icodes
	replace ch_age6mo=ch_age6mo+1
	label define ch_age6mo 1 "0 to 5.9"
	local counter=2 
	forvalue j=6(6)60 {
		local upper=`j'+6-.1
		lab def ch_age6mo `counter' "`j' to `upper'", add 
		local counter=`counter'+1
	}
	label values ch_age6mo ch_age6mo
	lab var ch_age6mo "Age groups (6 mo) index child" 

	* 3 months *
	egen ch_age3mo=cut(ch_agemo), at(0(3)60) icodes
	replace ch_age3mo=ch_age3mo+1
	label define ch_age3mo 1 "0 to 2.9" 
	local counter=2 
	forvalue j=3(3)60{
		local upper=`j'+3-.1
		lab def ch_age3mo `counter' "`j' to `upper'", add
		local counter=`counter'+1
	}
	label values ch_age3mo ch_age3mo
	lab var ch_age3mo "Age groups (3 mo) index child"
	
	* 1 month *
	egen ch_age1mo=cut(ch_agemo), at(0(1)60) icodes
	replace ch_age1mo=ch_age1mo+1
	lab def ch_age1mo 1 "0 to 0.9"
	local counter=2
	forvalue j=1(1)60 {
		local upper=`j'+1-.1
		lab def ch_age1mo `counter' "`j' to `upper'", add
		local counter=`counter'+1
	}
	lab values ch_age1mo ch_age1mo
	lab var ch_age1mo "Age groups (1 mo) index child" 
	

*** Create Zscores ****
	
	zscore06, a(ch_agemo) h(ch_height) w(ch_weight) s(ch_sex) female(0) male(1)
		
	rename haz06 ch_haz06
	rename waz06 ch_waz06
	rename whz06 ch_whz06
	rename bmiz06 ch_bmi06
	
 
*** New Vars *** 

*** anemic ***
	tab s24_q9, m
	replace s24_q9=. if s24_q9>70

	gen anemic=0 if !missing(s24_q9)
	replace anemic=1 if s24_q9<11
	lab var anemic "Child is anemic" 
		
	gen anemic_severe=0 if !missing(s24_q9)
	replace anemic_severe=1 if s24_q9<7
	lab var anemic_severe "Child is severely anemic"

*** stunting *** 
	tab ch_haz06, m
	
	gen stunted=0 if !missing(ch_haz06)
	replace stunted=1 if ch_haz06<-2
	lab var stunted "Child is stunted" 
	
*** underweight ***
	tab ch_waz06, m 
	
	gen underweight=0 if !missing(ch_waz06)
	replace underweight=1 if ch_waz06<-2
	lab var underweight "Child is underweight"
	
*** wasting *** 	
	tab ch_whz06, m
	
	gen wasting=0 if !missing(ch_whz06)
	replace wasting=1 if ch_whz06<-2
	lab var wasting "Child is wasting"

	
	
exit
